<template>
  <div class="login-container">
    <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="login-form" auto-complete="on" label-position="left">
      <div class="title-container">
        <h3 class="title">申请入驻</h3>
      </div>
      <el-tooltip :manual="true" v-model="tips.username" class="item" effect="dark" content="用户名必须是4-20个字符，只能包括数字和字母" placement="right">
        <el-form-item :error="usernameErrorMsg" prop="username">
          <span class="svg-container svg-container_login">
            <svg-icon icon-class="user" />
          </span>
          <el-input v-model="registerForm.username" name="username" type="text" auto-complete="on" placeholder="请输入用户名" @focus="tips.username=true" @blur="tips.username=false"/>
        </el-form-item>
      </el-tooltip>
      <el-tooltip :manual="true" v-model="tips.email" class="item" effect="dark" content="请填写邮箱，推荐使用新浪、QQ、163" placement="right">
        <el-form-item :error="emailErrorMsg" prop="email">
          <span class="svg-container svg-container_login">
            <svg-icon icon-class="email" />
          </span>
          <el-input v-model="registerForm.email" name="email" type="text" auto-complete="on" placeholder="请输入电子邮箱" @focus="tips.email=true" @blur="tips.email=false"/>
        </el-form-item>
      </el-tooltip>
      <el-tooltip :manual="true" v-model="tips.password" class="item" effect="dark" content="密码为6-20个字符，请使用字母加数字或者下划线组合密码" placement="right">
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input :type="passwordType" v-model="registerForm.password" name="password" auto-complete="on" placeholder="请输入密码" @focus="tips.password=true" @blur="tips.password=false"/>
          <span class="show-pwd" @click="showPwd">
            <svg-icon icon-class="eye" />
          </span>
        </el-form-item>
      </el-tooltip>

      <el-tooltip :manual="true" v-model="tips.repassword" class="item" effect="dark" content="密码为6-20个字符，请使用字母加数字或者下划线组合密码" placement="right">
        <el-form-item prop="repassword">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input :type="passwordType" v-model="registerForm.repassword" auto-complete="on" placeholder="请再次输入密码" @focus="tips.repassword=true" @blur="tips.repassword=false"/>
          <span class="show-pwd" @click="showPwd">
            <svg-icon icon-class="eye" />
          </span>
        </el-form-item>
      </el-tooltip>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleRegister">立即入驻</el-button>
      <span style="float:right;color:#fff">已有账号？<router-link to="/login" style="color:#0099FF;">立即登录</router-link></span>
    </el-form>

  </div>
</template>

<script>
export default {
  name: 'Register',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length < 4 || value.length > 20) {
        callback(new Error('用户名长度应为4-20个字符'))
      } else if (!/^[0-9a-zA-Z]{1,}$/.test(value)) {
        callback(new Error('用户名只能由字母和数字组成'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6 || value.length > 20) {
        callback(new Error('密码长度应为6-20个字符'))
      } else if (!/^[0-9a-zA-Z_]{1,}$/.test(value)) {
        callback(new Error('密码只能由字母、数字和下划线组成'))
      } else {
        callback()
      }
    }

    const validateRepassword = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error('两次密码输入不一致'))
      } else {
        callback()
      }
    }
    const validateEmail = (rule, value, callback) => {
      if (!/^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/.test(value)) {
        callback(new Error('邮箱格式不正确'))
      } else {
        callback()
      }
    }
    return {
      registerForm: {
        username: '',
        password: '',
        email: ''
      },
      registerRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        repassword: [{ required: true, trigger: 'blur', validator: validateRepassword }],
        email: [{ required: true, trigger: 'blur', validator: validateEmail }]
      },
      passwordType: 'password',
      loading: false,
      tips: {
        username: false,
        password: false,
        repasswrod: false,
        email: false
      },
      usernameErrorMsg: '',
      emailErrorMsg: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
  },
  destroyed() {
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid && !this.loading) {
          this.loading = true
          this.$store.dispatch('Register', this.registerForm).then(() => {
            this.loading = false
            this.$router.push({ path: '/register-success?email=' + this.registerForm.email })
          }).catch(response => {
            if (response.code === '307') {
              this.usernameErrorMsg = response.message
            } else if (response.code === '308') {
              this.emailErrorMsg = response.message
            }
            this.$notify.error({
              title: '失败',
              message: response.message
            })
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg:#2d3a4b;
$light_gray:#eee;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 100%;
    min-width: 320px;
    max-width: 480px;
    padding: 20px 15px;
    margin: 80px auto;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      font-weight: 400;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
